using Gateway.Common.Models;
using Gateway.Domain.Entities;
using Gateway.IApplication.Department;
using Gateway.IApplication.Department.Input;
using Gateway.IRepositories;

namespace Gateway.Application;

/// <summary>
/// 部门服务
/// </summary>
public class DepartmentApp : IDepartmentApp
{
    private readonly IAppRepository<Department> _repository;
    private readonly IUnitOfWork _unitOfWork;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="repository"></param>
    /// <param name="unitOfWork"></param>
    public DepartmentApp(IAppRepository<Department> repository, IUnitOfWork unitOfWork)
    {
        _repository = repository;
        _unitOfWork = unitOfWork;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<string> Add(CreateDepartmentInput input)
    {
        string parentLevelCode = string.Empty;
        if (!string.IsNullOrWhiteSpace(input.ParentId))
        {
            var parentInfo = await _repository.GetAsync(input.ParentId);
            parentLevelCode = parentInfo.LevelCode ?? "";
        }

        var entity = new Department(departName: input.DepartName, parentId: input.ParentId, remark: input.Remark,
            seqNo: input.SeqNo, parentLevelCode: parentLevelCode);
        input.Disabled = input.Disabled ?? "N";
        entity.SetDisabled(input.Disabled);
        await _repository.AddAsync(entity);
        await _unitOfWork.CommitAsync();
        return entity.Id;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="MyException"></exception>
    public async Task<bool> Update(UpdateDepartmentInput input)
    {
        var entity = await _repository.GetAsync(input.DepartmentId);
        if (entity == null)
            throw new MyException("数据无效");
        string parentLevelCode = string.Empty;
        if (!string.IsNullOrWhiteSpace(input.ParentId) && !input.ParentId.Equals(entity.ParentId ?? ""))
        {
            var parentInfo = await _repository.GetAsync(input.ParentId);
            parentLevelCode = parentInfo.LevelCode ?? "";
        }

        entity.Update(departName: input.DepartName, parentId: input.ParentId, remark: input.Remark,
            seqNo: input.SeqNo, parentLevelCode);
        input.Disabled = input.Disabled ?? "N";
        entity.SetDisabled(input.Disabled);
        await _repository.UpdateAsync(entity);
        await _unitOfWork.CommitAsync();
        return true;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    /// <exception cref="MyException"></exception>
    public async Task<bool> Remove(string id)
    {
        var entity = await _repository.FirstOrDefaultAsync(p => p.Id.Equals(id));
        if (entity == null)
            throw new MyException("数据无效");
        entity.SetDeleted();
        await _repository.UpdateAsync(entity);
        await _unitOfWork.CommitAsync();
        return true;
    }
}